//
//  DDServiceController.m
//  DingDongXiChe
//
//  Created by wtjr on 2017/11/24.
//  Copyright © 2017年 xieyingze. All rights reserved.
//

#import "DDPackageController.h"
#import "PackageBannerCell.h"
#import "PackageDetailCell.h"
#import "PackageTimeSelectCell.h"
#import "DDOrderController.h"
#import "PackageViewModel.h"
#import "DDAlertView.h"


@interface DDPackageController ()<UITableViewDelegate,UITableViewDataSource> {
    CGFloat _cellHeight;
    NSString *_selectTime;
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) BaseButton *nextButton;

@property (nonatomic, strong) PackageViewModel *viewModel;

@end

@implementation DDPackageController

#pragma mark - ********************生命周期********************

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - ********************配置信息********************

- (void)configuration {
    self.navTitle = @"套餐选择";
    _cellHeight = (kUI_HEIGHT - 64)/3;
    
    self.viewModel.model = self.model;
    [self.viewModel.getPackageListsCommand execute:nil];
}

- (void)initSubViews {
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.nextButton];
}

- (void)autoLayoutForSubViews {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(64, 0, kUI_ButtonHeight, 0));
    }];
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.height.equalTo(@(kUI_ButtonHeight));
    }];
}

- (void)bindViewModel {
    @weakify(self);
    [self.viewModel.updateTableSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
    [self.viewModel.getPackageListsCommand.errors subscribeNext:^(NSError * _Nullable x) {
        @strongify(self);
        [self showCustomAlerView:x.domain];
    }];
}

#pragma mark - ********************基础方法********************

- (void)showCustomAlerView:(NSString *)resultString {
    @weakify(self);
    DDAlertView *alert = [[DDAlertView alloc] initWithTitle:@"温馨提示" content:resultString ensureButtonTitle:nil cancelButtonTitle:@"返回"];
    alert.clickeCompleted = ^(NSInteger index) {
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    };
    [alert show];
}

#pragma mark - ********************代理方法********************

#pragma mark - UITableViewDataSource

//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

//返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        //套餐轮播图
        PackageBannerCell *cell = [PackageBannerCell cellWithTableView:tableView indexPath:indexPath];
        cell.viewModel = self.viewModel;
        WeakSelf;
        cell.clickCompleted = ^(PackageModel *model) {
            weakself.viewModel.currentPackageModel = model;
            [weakself.tableView reloadData];
        };
        return cell;
    } else if (indexPath.row == 1) {
        //套餐详情
        PackageDetailCell *cell = [PackageDetailCell cellWithTableView:tableView indexPath:indexPath];
        cell.viewModel = self.viewModel;
        return cell;
    }
    //时间选择器
    PackageTimeSelectCell *cell = [PackageTimeSelectCell cellWithTableView:tableView indexPath:indexPath];
    _selectTime = cell.pickerView.selectTime;
    cell.pickerView.selectCompleted = ^(NSString *time) {
        _selectTime = time;
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return kUI_is_Iphone5 ? _cellHeight - 25 : _cellHeight;
    } else if (indexPath.row == 1) {
        return kUI_is_Iphone5 ? _cellHeight + 25 : _cellHeight;
    }
    return _cellHeight - kUI_ButtonHeight;
}

#pragma mark - ********************响应事件********************
/**
 跳转到订单详情界面
 */
- (void)gotoOrderVC {
    DDOrderController *ctrl = [DDOrderController new];
    //商户id
    ctrl.viewModel.service_uuid = self.model.service_uuid;
    //套餐id
    ctrl.viewModel.package_id = self.viewModel.currentPackageModel.package_id;
    //套餐名称
    ctrl.viewModel.order_packageName = self.viewModel.currentPackageModel.package_name;
    //套餐时长
    ctrl.viewModel.order_packageDuration = self.viewModel.currentPackageModel.package_duration;
    //套餐价格
    ctrl.viewModel.order_packagePrice = self.viewModel.currentPackageModel.package_price;
    //套餐时间
    ctrl.viewModel.order_packageTime = _selectTime;
    //套餐已使用优惠券
    ctrl.viewModel.order_coupon = @"0.0";
    //套餐需付金额
    ctrl.viewModel.order_payPrice = self.viewModel.currentPackageModel.package_price;
    
    [self.navigationController pushViewController:ctrl animated:YES];
}
    

#pragma mark - ********************懒加载*********************

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = _cellHeight;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.bounces = NO;
    }
    return _tableView;
}

- (BaseButton *)nextButton {
    if (!_nextButton) {
        _nextButton = [BaseButton new];
        _nextButton.backgroundColor = KCOLOR_mainColor;
        [_nextButton setButtonStyle:@"下一步" titleColor:kCOLOR_white titleFont:kFONT_mainFont(16) texrAlignment:NSTextAlignmentCenter];
        [_nextButton addTarget:self action:@selector(gotoOrderVC) forControlEvents:UIControlEventTouchUpInside];
    }
    return _nextButton;
}

- (PackageViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [PackageViewModel new];
    }
    return _viewModel;
}

@end
